package com.baturu.vin.dfpv.service.impl;

import com.baturu.vin.base.service.EPCCacheRealTimeProcess;
import com.baturu.vin.dfpv.dal.dao.DfpvCarTypeDAO;
import com.baturu.vin.dfpv.dal.dao.DfpvVinDAO;
import com.baturu.vin.dfpv.dto.DfpvCarSystemBtrIdDTO;
import com.baturu.vin.dfpv.dto.DfpvVinDTO;
import com.baturu.vin.dfpv.dto.ResponseCarDTO;
import com.baturu.vin.kit.VinModelYearUtil;
import com.google.common.base.Splitter;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author: chenjiahao
 * @Time: 2018/8/31
 */

@Slf4j
@Service("dfpvEPCCacheRealTimeProcess")
public class DfpvEPCCacheRealTimeProcess implements EPCCacheRealTimeProcess {
    @Autowired
    DfpvVinDAO dfpvVinDAO;
    @Autowired
    DfpvCarTypeDAO dfpvCarTypeDAO;


    @Override
    public boolean buildVinAnalyticalResults(String vinCode, String analyticalResultText) throws Exception {
        DfpvVinDTO dfpvVinDTO = buildQueryCarResult(vinCode, analyticalResultText);
        if(!Objects.isNull(dfpvVinDTO)) {
            dfpvVinDTO.setVinCode(vinCode);
            String engineType = dfpvVinDTO.getEngineType();
            List<String> l = Splitter.on("，").splitToList(engineType);
            List<String> dis = Splitter.on(" ").splitToList(l.get(0));
            if(CollectionUtils.size(dis) > 1) {
                dfpvVinDTO.setDisplacement(dis.get(1));
            }else if(StringUtils.isNotEmpty(engineType)) {
                    String carSystem = "东风风神".concat(dfpvVinDTO.getCarSystem());
                    String transmissionType = dfpvVinDTO.getTransmissionType();
                    List<String> list = Splitter.on(",").splitToList(transmissionType);
                    List<DfpvCarSystemBtrIdDTO> dfpvCarTypeDTOs = dfpvCarTypeDAO.queryDisplacementByVinResult(carSystem, engineType, list);
                    if (CollectionUtils.isNotEmpty(dfpvCarTypeDTOs)) {
                        String displacement = dfpvCarTypeDTOs.get(0).getDisplacement();
                        dfpvVinDTO.setDisplacement(displacement);
                    }

            }
            dfpvVinDTO.setBtrBrand("东风风神");
            dfpvVinDTO.setBtrSubBrand("东风风神");
            String vinYearByVinCode = VinModelYearUtil.getVinYearByVinCode(vinCode);
            dfpvVinDTO.setMj(vinYearByVinCode);
            saveVinModel(dfpvVinDTO);
        }
        return true;
    }

    private int saveVinModel(DfpvVinDTO dfpvVinDTO) {
        String vinCode = dfpvVinDTO.getVinCode();
        DfpvVinDTO vinModel = dfpvVinDAO.findVinModel(vinCode);
        if(Objects.isNull(vinModel)) {
            return dfpvVinDAO.insert(dfpvVinDTO);
        }
        return 0;
    }

    private DfpvVinDTO buildQueryCarResult(String vinCode, String analyticalResultText) {
        DfpvVinDTO dfpvVinDTO = null;

        try {
            if (StringUtils.isEmpty(analyticalResultText)) {
                return null;
            }
            Gson gson = new Gson();
            ResponseCarDTO carDTO = gson.fromJson(analyticalResultText, ResponseCarDTO.class);
            if(Objects.isNull(carDTO)) {
                return null;
            }
            String status = carDTO.getStatus();
            if (StringUtils.equals(status, "ok")) {
                dfpvVinDTO = carDTO.getVininfo();
            }

        } catch (Exception e) {
            log.error("vin{} 查询结果转换异常{}", vinCode, e);
        }


        return dfpvVinDTO;
    }
}
